* Reset settings and initialize log file
launch, path("share/between")

*-------------------------------------------------------------------------------
* Price and Wasserman (2024), "The Summer Drop in Female Employment"
*
* Description: Report summary statistics illustrating "between-job" effects.
*-------------------------------------------------------------------------------


* Report education sector's share of employment in the full sample
*-------------------------------------------------------------------------------

* Load the estimation sample
gzuse "$basepath/data/derived/cps_bms_sample.dta.gz", clear
keep pid tm year month wtfinl wtraked linked_monthly female emp school occ1990 occ1990_2d

* Report education shares as of May
tab school if female == 0 & emp == 1 & month == 5 [aw = wtfinl]
tab school if female == 1 & emp == 1 & month == 5 [aw = wtfinl]


* Load data on individuals observed in both May and July
*-------------------------------------------------------------------------------

* Retain individuals who are observed in May, June, and July in a given year
keep if inlist(month, 5, 6, 7)
bysort pid year (month): keep if _N == 3

* Restrict to those with valid longitudinal links
bysort pid year (month): keep if linked_monthly[2] == 1 & linked_monthly[3] == 1

* Restrict to those employed as of May
bysort pid year (month): keep if emp[1] == 1

* Record sector/occupation as of May
bysort pid year (month): replace school = school[1]
bysort pid year (month): replace occ1990 = occ1990[1]
bysort pid year (month): replace occ1990_2d = occ1990_2d[1]

* Keep one observation per person x year
keep if month == 7

* Record separations from May to July
gen byte sep = (emp == 0)


* Classify workers by sector and occupation
*-------------------------------------------------------------------------------

* Tag all workers
gen byte overall = 1

* Tag non-education workers
gen non_school = (school == 0)

* Tag select occupations within education
gen byte occ_elemteach = (school == 1 & occ1990 == 156)
gen byte occ_highteach = (school == 1 & occ1990 == 157)
gen byte occ_collteach = (school == 1 & inrange(occ1990, 113, 154))
gen byte occ_othteach  = (school == 1 & inlist(occ1990, 155, 158, 159, 163))
gen byte occ_manager   = (school == 1 & inrange(occ1990_2d, 0, 1))
gen byte occ_admin     = (school == 1 & inrange(occ1990_2d, 22, 31))
gen byte occ_service   = (school == 1 & inlist(occ1990_2d, 35, 37, 70))
egen check = rowtotal(occ_*)
assert inlist(check, 0, 1)

* Tag other occupations within education
gen byte occ_other = (school == 1 & check == 0)


* Compute statistics
*-------------------------------------------------------------------------------

foreach v of varlist overall school non_school occ_* {
	if regexm("`v'", "^occ_") {
		local vv = substr("`v'", 5, .)
	}
	else {
		local vv `v'
	}

	foreach f of numlist 0 1 {
		if inlist("`v'", "school", "non_school") {
			* Share employed in each sector as of May
			sum `v' if female == `f' [aw = wtraked]
			local sh_`vv'_f`f' : display %4.1f 100 * r(mean)
		}
		else {
			* Share of education workers employed in each occupation as of May
			sum `v' if female == `f' & school == 1 [aw = wtraked]
			local sh_`vv'_f`f' : display %4.1f 100 * r(mean)
		}

		* Hazard rate of separation from this type of job, by sex
		sum sep if female == `f' & `v' == 1 [aw = wtraked]
		local hz_`vv'_f`f' = 100 * r(mean)
	}

	* Hazard rate of separation from this type of job, averaging female and male hazards
	local hz_`vv' : display %4.1f (`hz_`vv'_f0' + `hz_`vv'_f1')/2
}

* Sum of sectoral shares
local tot_sec_f1: display %4.1f `sh_school_f1' + `sh_non_school_f1'
local tot_sec_f0: display %4.1f `sh_school_f0' + `sh_non_school_f0'

* Sum of occupational shares within education
local tot_occ_f1: display %4.1f `sh_elemteach_f1' + `sh_highteach_f1' + `sh_othteach_f1' + `sh_collteach_f1' + `sh_admin_f1'  + `sh_manager_f1'  + `sh_service_f1' + `sh_other_f1'
local tot_occ_f0: display %4.1f `sh_elemteach_f0' + `sh_highteach_f0' + `sh_othteach_f0' + `sh_collteach_f0' + `sh_admin_f0'  + `sh_manager_f0'  + `sh_service_f0' + `sh_other_f0'

* Confirm addivity to rounding and impose exact additivity
assert abs(`tot_occ_f1' - 100) <= .1
assert abs(`tot_occ_f0' - 100) <= .1

local tot_occ_f1 "100.0"
local tot_occ_f0 "100.0"


* Create table
*-------------------------------------------------------------------------------

* Write table header
file open textable using "$basepath/output/between.tex", write replace
file write textable "\begin{tabular}{lrrr}"  _n
file write textable "\toprule" _n
file write textable "& \multicolumn{2}{c}{\; \textbf{\% in sector/occ}} & \multicolumn{1}{c}{\; \textbf{Pr(E $\rightarrow$ N)}} \\ " _n
file write textable "&  {\centering \; \textbf{Women}} & {\centering \; \textbf{Men}} &  \\ " _n
file write textable "\midrule \\[-0.5em]" _n

* Sectoral shares and hazards
file write textable "\emph{Sector:} \\ "_n
file write textable "\quad (1) \; Education & `sh_school_f1' & `sh_school_f0' & `hz_school' \\ " _n
file write textable "\quad (2) \; Non-education & `sh_non_school_f1' & `sh_non_school_f0' & `hz_non_school' \\ " _n
file write textable "\hspace{2.8em} \emph{Total} & `tot_sec_f1' & `tot_sec_f0' & `hz_overall' \\ \\" _n

* Occupational shares and hazards within the education sector
file write textable "\emph{Occupation in education sector:} \\ "_n
file write textable "\quad (3) \; Primary school teacher & `sh_elemteach_f1' & `sh_elemteach_f0' & `hz_elemteach' \\ " _n
file write textable "\quad (4) \; Secondary school teacher & `sh_highteach_f1' & `sh_highteach_f0' & `hz_highteach' \\ " _n
file write textable "\quad (5) \; Other non-college teacher & `sh_othteach_f1' & `sh_othteach_f0' & `hz_othteach' \\ " _n
file write textable "\quad (6) \; College teacher & `sh_collteach_f1' & `sh_collteach_f0' & `hz_collteach' \\ " _n
file write textable "\quad (7) \; Administrative staff & `sh_admin_f1' & `sh_admin_f0' & `hz_admin' \\ " _n
file write textable "\quad (8) \; Managers & `sh_manager_f1' & `sh_manager_f0' & `hz_manager' \\ " _n
file write textable "\quad (9) \; Food/trans./cleaning services & `sh_service_f1' & `sh_service_f0' & `hz_service' \\ " _n
file write textable "\hspace{0.5em}(10) \; Other & `sh_other_f1' & `sh_other_f0' & `hz_other' \\ " _n
file write textable "\hspace{2.8em} \emph{Total education} & `tot_occ_f1' & `tot_occ_f1' & `hz_school' \\ " _n

* Write table footer
file write textable "\bottomrule" _n
file write textable "\end{tabular}"
file close textable

* Close the log file
unlaunch
